package com.ins1st.plus.service.system.user.impl;

import cn.hutool.crypto.SecureUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ins1st.plus.api.system.menu.service.SysMenuApi;
import com.ins1st.plus.api.system.role.entity.SysRoleMenu;
import com.ins1st.plus.api.system.role.entity.SysUserRole;
import com.ins1st.plus.api.system.role.service.SysRoleMenuApi;
import com.ins1st.plus.api.system.role.service.SysUserRoleApi;
import com.ins1st.plus.api.system.user.entity.SysUser;
import com.ins1st.plus.api.system.user.service.SysUserApi;
import com.ins1st.plus.base.ServiceResult;
import com.ins1st.plus.service.system.user.mapper.SysUserMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: ins1st-plus
 * @description: 用户业务实现类
 * @author: coderSun
 * @create: 2019-09-01 21:48
 **/
@Service(interfaceClass = SysUserApi.class, timeout = 3000)
@Component
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserApi {

    @Autowired
    private SysUserRoleApi sysUserRoleApi;
    @Autowired
    private SysRoleMenuApi sysRoleMenuApi;
    @Autowired
    private SysMenuApi sysMenuApi;


    @Override
    public IPage<SysUser> page(SysUser sysUser, Page page) {
        QueryWrapper qw = new QueryWrapper();
        qw.eq(sysUser.getId() != null, "id", sysUser.getId());
        qw.eq(StringUtils.isNotBlank(sysUser.getUserMobile()), "user_mobile", sysUser.getUserMobile());
        qw.like(StringUtils.isNotBlank(sysUser.getUserName()), "user_name", sysUser.getUserName());
        return this.baseMapper.selectPage(page, qw);
    }


    @Override
    public ServiceResult update(SysUser sysUser) {
        SysUser old = this.baseMapper.selectById(sysUser.getId());
        if (StringUtils.isNotBlank(sysUser.getUserPassword())) {
            if (!old.getUserPassword().equals(sysUser.getUserPassword())) {
                sysUser.setUserPassword(SecureUtil.md5(sysUser.getUserName() + "-" + sysUser.getUserPassword()));
            }
        }
        this.baseMapper.updateById(sysUser);
        return ServiceResult.success();
    }


    @Override
    public SysUser getOne(SysUser sysUser) {
        QueryWrapper qw = new QueryWrapper();
        qw.setEntity(sysUser);
        return this.baseMapper.selectOne(qw);
    }

    @Override
    public List<String> queryMenuRolesByUserId(Integer userId) {
        List<SysUserRole> sysUserRoleList = sysUserRoleApi.list(new QueryWrapper<SysUserRole>().eq("user_id", userId));
        List<String> result = new ArrayList<>();
        for (SysUserRole sysUserRole : sysUserRoleList) {
            List<SysRoleMenu> sysRoleMenuList = this.sysRoleMenuApi.list(new QueryWrapper<SysRoleMenu>()
                    .eq("role_id", sysUserRole.getRoleId()));
            for (SysRoleMenu sysRoleMenu : sysRoleMenuList) {
                result.add(this.sysMenuApi.getById(sysRoleMenu.getMenuId()).getRole());
            }
        }
        return result;
    }
}
